package com.aditya.personalexpense.adapters;

import java.util.Vector;

import android.app.AlertDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.aditya.personalexpense.R;
import com.aditya.personalexpense.dataaccess.CurrencyDA;
import com.aditya.personalexpense.dataobject.CurrencyDO;

public class CurrencyAdapter extends BaseAdapter
{
	private Context context;
	private Vector<CurrencyDO> vecCurrencyDO;
	private boolean setViewIcon = true;
	private boolean setViewClicable = true;
	
	public CurrencyAdapter(Context context, Vector<CurrencyDO> vecCurrencyDO) 
	{
		this.context = context;
		this.vecCurrencyDO = vecCurrencyDO;
	}
	
	public void refresh(Vector<CurrencyDO> vecCurrencyDO)
	{
		this.vecCurrencyDO = vecCurrencyDO;
		notifyDataSetChanged();
	}
	
	public void setViewIcon(boolean setViewIcon)
	{
		this.setViewIcon = setViewIcon;
	}
	
	public void setViewClicable(boolean setViewClicable)
	{
		this.setViewClicable = setViewClicable;
	}
	
	@Override
	public int getCount() 
	{
		return vecCurrencyDO.size();
	}

	@Override
	public CurrencyDO getItem(int position) {
		return vecCurrencyDO.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) 
	{
		convertView = LayoutInflater.from(context).inflate(R.layout.currency_item, null);
		TextView tvItemName = (TextView) convertView.findViewById(R.id.tvItemName);
		ImageView ivItem = (ImageView) convertView.findViewById(R.id.ivItem);
		tvItemName.setText(vecCurrencyDO.get(position).simbol + " "+vecCurrencyDO.get(position).name);
		
		if(setViewIcon)
			ivItem.setVisibility(View.VISIBLE);
		else
			ivItem.setVisibility(View.GONE);
		
		if(setViewClicable)
		{
			convertView.setOnClickListener(new OnClickListener() 
			{
				@Override
				public void onClick(View v) 
				{
					final AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
					LinearLayout view = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.add_new_currency_dialog, null); 
					alertDialog.setView(view);
					final AlertDialog dialog = alertDialog.create();
					final TextView tvtitle = (TextView) view.findViewById(R.id.tvtitle);
					tvtitle.setText("Modify Currency");
					final EditText etCurrency = (EditText) view.findViewById(R.id.etCurrency);
					final EditText etCurrencySymbol = (EditText) view.findViewById(R.id.etCurrencySymbol);
					etCurrency.setText(vecCurrencyDO.get(position).name);
					etCurrencySymbol.setText(vecCurrencyDO.get(position).simbol);
					
					Button btnOk = (Button) view.findViewById(R.id.btnOk);
					Button btnCancel = (Button) view.findViewById(R.id.btnCancel);
					btnOk.setOnClickListener(new OnClickListener() 
					{
						@Override
						public void onClick(View v) 
						{
							if(etCurrency.getText().toString().equalsIgnoreCase("") || etCurrencySymbol.getText().toString().equalsIgnoreCase(""))
							{
								Toast.makeText(context, "Please enter the complete information!", Toast.LENGTH_SHORT).show();
							}
							else
							{
								final String currency = etCurrency.getText().toString();
								final String currencySymbol  = etCurrencySymbol.getText().toString();
							
								new Thread(new Runnable() {
									
									@Override
									public void run() 
									{
										CurrencyDO currencyDO = new CurrencyDO();
										currencyDO.name = currency;
										currencyDO.simbol = currencySymbol;
										currencyDO.currencyid = vecCurrencyDO.get(position).currencyid;
										
										new CurrencyDA().updateCurrency(currencyDO);
										dialog.dismiss();
										refresh(new CurrencyDA().getAllCurrency());
											
									}
								}).start();
							}
						}
					});
					btnCancel.setOnClickListener(new OnClickListener() 
					{
						
						@Override
						public void onClick(View v) 
						{
							dialog.dismiss();
						}
					});
					dialog.show();
					
				}
			});
		}
		return convertView;
	}
	
}